package br.com.management.mentor.view.users;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import br.com.management.mentor.controller.auth.UserController;
import br.com.management.mentor.model.entities.auth.Permission;
import br.com.management.mentor.model.entities.auth.User;
import br.com.management.mentor.model.repositories.PermissionsRepository;
import br.com.management.mentor.view.core.View;

@Component
public class Create extends View {

	private Composite parent;
	
	@Autowired
	private Locale locale;
	
	@Autowired
	private ApplicationContext context; 
	
	@Autowired
	private PermissionsRepository permissionsRepository;
	
	@Autowired
	private UserController userController;
	
	@Override
	public void build( Composite parent ) {
		this.parent = parent;
		
		Group g1 = new Group( parent , SWT.NONE );
		
		GridLayout gridLayout = new GridLayout( 2 , true );
		g1.setLayout( gridLayout );
		
		Label userNameLabel = new Label( g1 , SWT.LEFT );
		userNameLabel.setText( "Usuário" );
		final Text userNameText = new Text( g1 , SWT.SINGLE );
		
		Label passwordLabel = new Label( g1 , SWT.LEFT );
		passwordLabel.setText( "Senha" );
		final Text passwordText = new Text( g1 , SWT.SINGLE | SWT.PASSWORD );
		
//		Label passwordConfirmationLabel = new Label( g1 , SWT.LEFT );
//		passwordConfirmationLabel.setText( "Confirmação de Senha" );
//		Text passwordConfirmationText = new Text( g1 , SWT.SINGLE | SWT.PASSWORD );
		
		Group g2 = new Group( parent , SWT.None );
		
		GridLayout gridLayout2 = new GridLayout( 1 , true );
		g2.setLayout( gridLayout2 );
		
		Label permissionsLabel = new Label( g2 , SWT.LEFT );
		permissionsLabel.setText( "Permissões" );
		
		List<Permission> permissions = permissionsRepository.getAll();
		final Collection<Button> permissionsBtns = new ArrayList<>( permissions.size() );
		for ( Permission permission : permissions ) {
			String key = String.format( "%s.%s" , Permission.class.getSimpleName().toLowerCase() , permission.getName().toLowerCase() );
			
			Button b = new Button( g2 , SWT.CHECK );
			b.setText( context.getMessage( key , null , locale ) );
			b.setData( permission );
			permissionsBtns.add( b );
		}
		
		Button createBtn = new Button( parent , SWT.PUSH );
		createBtn.setText( "Cadastrar" );
		
		createBtn.addSelectionListener( new SelectionListener() {
			
			@Override
			public void widgetSelected( SelectionEvent e ) {
				User u = new User();
				
				u.setUsername( userNameText.getText() );
				u.setPassword( passwordText.getText() );
				
				Set<Permission> selectedPermissions = new HashSet<>();
				for ( Button b : permissionsBtns ) {
					if( b.getSelection() ) selectedPermissions.add( (Permission) b.getData() );
				}
				u.setPermissions( selectedPermissions );
				
				userController.create( u );
			}
			
			@Override
			public void widgetDefaultSelected( SelectionEvent e ) { }
			
		} );
	}

	@Override
	public Shell getParent() {
		return parent.getShell();
	}

}